/*
 *     This file is part of Neptune Network Emulator.
 *
 *  Neptune Network Emulator is free software: 
 *  you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Neptune Network Emulator is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Neptune Network Emulator.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/**
 *
 */
package neptune.web.client.callback;

import neptune.web.client.WebInterface;
import neptune.web.client.model.ExperimentInfos;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentView;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.LongMessageWidget;
import neptune.web.client.view.components.MessageWidget;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
 *
 */
public class XMLtopologyUploadCallback implements AsyncCallback<ExperimentInfos> 

{
	
	private String expName;
	private PopupPanel loadingPopup = null;
	
	public XMLtopologyUploadCallback(String expName)
	{
		super();
		this.expName = expName;
	}
	
	public XMLtopologyUploadCallback(String expName2, PopupPanel loadingPopup)
	{
		expName = expName2;
		this.loadingPopup = loadingPopup;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
	 */
	public void onFailure(Throwable caught) 
	{
		if (loadingPopup != null)
			loadingPopup.hide();

		WebInterface.addMessageToConsoleAndShowDialog(
				"Error while validating topology for experiment '" + expName
						+ "'. Cause:\n" + caught.getMessage(), true);
		
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.rpc.AsyncCallback#onSuccess(java.lang.Object)
	 */
	public void onSuccess(ExperimentInfos result) 
	{
		if (loadingPopup != null)
			loadingPopup.hide();

		// Imposta lo stato dell'esperimento
		SuperView operative = (SuperView) ViewManager.getViewManager()
				.getViewByName(OperativeView.DEFAULT_TOKEN);

		ExperimentView expView = (ExperimentView) operative
				.getViewByName(ExperimentView.DEFAULT_TOKEN);

		expView.setExperimentState(result.experimentStatus);

		// Crea il messaggio sulla validazione della topologia
		String message = "Topology uploading completed.\n"
			+ "WARNING: Only if the validation process gives a VALID result "
			+ "the topology is actually saved on the server."
			+ "\n\n\n"
				+ "Topology validation result: " + result.topologyStatus;
		WebInterface.showMessageDialog(new LongMessageWidget(
				MessageWidget.MESSAGE, message + "\nTopology validation log:\n"
						+ result.topologyLog), true);
		ConsoleManager.addConsoleMessage(message, false);
		
	}

}
